package com.simpleframe.persistence.hibernate;

public class OrderBy {
	
	public static String desc(String field,String...fields){
		StringBuffer hql = new StringBuffer(" order by");
		hql.append(" "+getFirstField(field));
		for (String f : fields) {
			if(checkField(f))
				hql.append(", "+f);
		}
		hql.append(" desc");
		return hql.toString();
	}
	
	public static String asc(String field,String...fields){
		StringBuffer hql = new StringBuffer(" order by");
		hql.append(" "+getFirstField(field));
		for (String f : fields) {
			if (checkField(f)) 
				hql.append(", "+f);
		}
		hql.append(" asc");
		return hql.toString();
	}
	
	private static String getFirstField(String field){
		if(field==null || field.equals("")){
			return "id";
		}
		return field;
	}
	
	private static boolean checkField(String field){
		if(field==null || field.equals("")){
			return false;
		}
		return true;
	}
}
